package engine.game;

import static org.lwjgl.opengl.GL11.* ; 

import java.io.Serializable;

import engine.Engine;
import engine.graphics.*;
import engine.gui.MainMenuScreen;
import engine.gui.Panel;
import engine.gui.PauseMenuScreen;
import engine.input.IInput;
import engine.objects.PhysicsObject;
import engine.system.IInit;
import engine.system.Timer;

public abstract class Game implements IInit, IInput, IRender, Serializable {

	private static final long serialVersionUID = 1L;
	
	protected final String NAME ; 
	
	protected ESaveLoad sl = ESaveLoad.SAVE;
	protected boolean canSave = false;
	protected final Engine engine;
	
	private Panel backgroundScreen = null ; 
	
	private EGameMode mode = EGameMode.MENU;
	protected MainMenuScreen mainMenu;
	protected PauseMenuScreen pauseMenu;
	
	protected Level level;
	
	public Level getLevel(){
		return level ; 
	}

	public Game(Engine e, String name) {
		this.engine = e;
		this.NAME = name ; 
		backgroundScreen = new Panel(null, 0, 0, Engine.WIDTH, Engine.HEIGHT);
		backgroundScreen.changeNormalColor(new Color4f(0.15f, 0.15f, 0.15f, 1.0f));
	}
	
	public String getName(){
		return this.NAME ; 
	}
	
	protected void initLevel(){
//		System.out.println(this.levelInitialised());
//		this.level.initGUI(); 
		this.continueGame();
	}

	public boolean levelInitialised() {
		return !(this.level == null);
	}

	public boolean isPaused() {
		return this.mode == EGameMode.PAUSED;
	}

	public boolean isRunning() {
		return this.mode != EGameMode.MENU;
	}

	public void pause() {
		this.mode = EGameMode.PAUSED;
		if (level != null && level.gameObjects != null && level.gameObjects.getObjects() != null)
			for (PhysicsObject o : level.gameObjects.getObjects())
				if (o != null && o.getFallingTimer() != null)
					o.stopAllTimers();
	}

	public void exit() {
		this.mode = EGameMode.MENU;
		this.level = null ; 
		new Timer().delay(Timer.EVENT_TIMEOUT);
	}

	public void continueGame() {
		this.mode = EGameMode.GAME;
		if(level != null && level.gameObjects != null && level.gameObjects.getObjects() != null)
			for(PhysicsObject o: level.gameObjects.getObjects())
				if(o != null && o.getFallingTimer() != null)
					o.continueAllTimers(); 
	}
	
	public MainMenuScreen mainMenu() {
		return this.mainMenu;
	}

	public PauseMenuScreen pauseMenu(){
		return this.pauseMenu ; 
	}
	
	@Override
	public void getInput() {
		if (this.isRunning()) {
			if (this.isPaused())
				pauseMenu.getInput();
			else{
				if (!this.levelInitialised())
					initLevel();
				level.getInput();
			}
		} else
			this.mainMenu().getInput();
	}

	@Override
	public void update() {
		if (this.isRunning()) {
			if (this.isPaused()){
				pauseMenu.update();
			}else {
				if (!this.levelInitialised())
					initLevel();
				level.update();
			}
		}else {
			this.backgroundScreen.update(); 
			this.mainMenu().update();
		}
	}

	@Override
	public void render() {
		if (this.isRunning()) {
			if (!this.levelInitialised())
				initLevel();
			glPushMatrix();
			level.render();
			glPopMatrix();
			if (this.isPaused()){
//				MADGL.disableLighting(); 
				pauseMenu.render();
//				MADGL.enableLighting(); 
			}
		} else{ 
//			MADGL.disableLighting(); 
			this.backgroundScreen.render();
			this.mainMenu().render();
//			MADGL.enableLighting();
		}
	}

}
